package org.testmyapp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.dao.DataAccessResourceFailureException;
import org.testmyapp.entities.Bill;
import org.testmyapp.entities.Claim;
import org.testmyapp.entities.ClaimDisqualifictation;
import org.testmyapp.entities.ClaimType;
import org.testmyapp.entities.Contract;
import org.testmyapp.entities.Coverage;
import org.testmyapp.entities.InsuredPerson;
import org.testmyapp.entities.Retention;
import org.testmyapp.entities.TarifDefinition;
import org.testmyapp.entities.TariffInstance;
import org.testmyapp.entities.WaitingTime;
import org.testmyapp.persistence.BusinessObjectPersistenceProvider;

@Named
@ApplicationScoped
// Vertragskomponente
public class ContractComponentService {

	@Inject
	private BusinessObjectPersistenceProvider bopp;

	public Contract loadContract(String contractId) {
		String sql = "select a from " + Contract.tableName + " a where a.id = :contractId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("contractId", Integer.parseInt(contractId));
		try {
			Contract contract = bopp.querySingle(sql, params, Contract.class);
			return contract;
		} catch (Exception e) {
			throw new DataAccessResourceFailureException("Couldn't load object");
		}
	}

	public List<InsuredPerson> loadInsuredPersons(Contract contract) {
		String sql = "select a from " + InsuredPerson.tableName + " a where a.contract = :contract_id_fk";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("contract_id_fk", contract);
		try {
			List<InsuredPerson> listInsuredPerson = bopp.queryList(sql, params, InsuredPerson.class);
			return listInsuredPerson;
		} catch (Exception e) {
			throw new DataAccessResourceFailureException("Couldn't load object");
		}
	}

	public List<TariffInstance> loadTarifInstancesForIP(InsuredPerson ip) {
		String sql = "select a from " + TariffInstance.tableName + " a where a.ip = :ipfk";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ipfk", ip);
		try {
			List<TariffInstance> listTariffs = bopp.queryList(sql, params, TariffInstance.class);
			return listTariffs;
		} catch (Exception e) {
			throw new DataAccessResourceFailureException("Couldn't load object");
		}
	}

	public List<Coverage> loadTarifCoverage(TarifDefinition tarif) {
		String sql = "select a from " + Coverage.tableName + " a where a.tarifDefinition = :tarif_fk";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tarif_fk", tarif);
		try {
			List<Coverage> coverageList = bopp.queryList(sql, params, Coverage.class);
			return coverageList;
		} catch (Exception e) {
			throw new DataAccessResourceFailureException("Couldn't load object");
		}
	}

	public List<Claim> loadClaims(ClaimType claimTypeForTarif) {
		String sql = "select a from " + Claim.tableName + " a where a.claimType = :claimType_fk";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("claimType_fk", claimTypeForTarif);
		try {
			List<Claim> claimList = bopp.queryList(sql, params, Claim.class);
			return claimList;
		} catch (Exception e) {
			throw new DataAccessResourceFailureException("Couldn't load object");
		}
	}

	public Claim loadClaimByNameAndCatalog(String claim, String claimCatalog) {
		String sql = "select a from " + Claim.tableName + " a where a.identifier = :cl and a.catalog = :ca";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("cl", claim);
		params.put("ca", claimCatalog);
		
		try {
			Claim claimObj = bopp.querySingle(sql, params, Claim.class);
			return claimObj;
		} catch (Exception e) {
			throw new DataAccessResourceFailureException("Couldn't load object");
		}
	}

	public List<WaitingTime> loadWaitingTimesForTarif(TariffInstance tariffInstance) {
		String sql = "select a from " + WaitingTime.tableName + " a where a.tariffInstance = :tariffInstance_fk and a.valid = :validState";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tariffInstance_fk", tariffInstance);
		params.put("validState", true);
		try {
			List<WaitingTime> waitingTimeList = bopp.queryList(sql, params, WaitingTime.class);
			return waitingTimeList;
		} catch (Exception e) {
			throw new DataAccessResourceFailureException("Couldn't load object");
		}
	}

	public List<ClaimDisqualifictation> loadClaimDisqualificationsForTarifAndClaim(TariffInstance ti, Claim claim) {
		String sql = "select a from " + ClaimDisqualifictation.tableName
				+ " a where a.tariffInstance = :tariffInstance_fk and a.claim = :claim_fk";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tariffInstance_fk", ti);
		params.put("claim_fk", claim);
		try {
			List<ClaimDisqualifictation> claimDisq = bopp.queryList(sql, params, ClaimDisqualifictation.class);
			return claimDisq;
		} catch (Exception e) {
			throw new DataAccessResourceFailureException("Couldn't load object");
		}
	}

	public List<Retention> loadRetentionForTarifInstanceAndCoverage(TariffInstance ti, Coverage c) {
		String sql = "select a from " + Retention.tableName
				+ " a where a.tariffInstance = :tariffInstance_fk and a.coverage = :coverage_fk";

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tariffInstance_fk", ti);
		params.put("coverage_fk", c);

		try {
			List<Retention> retentions = bopp.queryList(sql, params, Retention.class);
			return retentions;
		} catch (Exception e) {
			throw new DataAccessResourceFailureException("Couldn't load object");
		}
	}

}
